home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Grant's CGI Framework 1.0b14 / MyCGIProcess.c < prev    next >
Text File  |  1996-04-12  |  5KB  |  147 lines

  1. /*****
  2.  *
  3.  *    Grant's CGI Shell (Common Grant Interface :-)
  4.  *        http://arpp.carleton.ca/grant/mac/grantscgi/
  5.  *
  6.  *    MyCGIProcess.c
  7.  *
  8.  *    Sample application using the cgi interface.
  9.  *
  10.  *    CustomCGIProcess is where you will do your application specific processing
  11.  *  of the cgi stuff.
  12.  *
  13.  *    by Grant Neufeld
  14.  *
  15.  *    Copyright ©1995,1996 by Grant Neufeld
  16.  *
  17.  *    http://arpp.carleton.ca/grant/
  18.  *    gneufeld@ccs.carleton.ca
  19.  *    grant@acm.org
  20.  *
  21.  *    This source may be freely used as long as the copyright notice is kept in the source.
  22.  *    I ask that you let me know of any enhancements (read: bug fixes) to this code.
  23.  *    I would also like copies of (or discounts on) anything you produce using this code, please.
  24.  *
  25.  *****/
  26.  
  27. #include "MyConfiguration.h"
  28. #if kCompileWithCGICode
  29.  
  30. #include <string.h>
  31. #include <Threads.h>
  32.  
  33. #include "compiler_stuff.h"
  34. #include "globals.h"
  35.  
  36. #include "CGI.h"
  37. #include "DebugUtil.h"
  38. #include "LogUtil.h"
  39. #include "MemoryUtil.h"
  40. #include "ProcessUtil.h"
  41.         
  42.  
  43. /***  CUSTOM CGI FUNCTION  ***/
  44.  
  45. /* This function is where the CGI is actually processed.
  46.     You should replace its contents with your own.
  47.     You need to allocate (*theCGIHandle)->responseData using MemoryNewHandle.
  48.     Put an HTTP header at the beginning of (*theCGIHandle)->responseData.
  49.     Put your data immediately following the last character of the HTTP header.
  50.     (*theCGIHandle)->responseData will be automatically deallocated by the CGI handler.
  51.     You should set (*theCGIHandle)->responseSize to be the size of the responseData,
  52.      including null terminator if you have one.
  53.     
  54.     (this function's prototype is defined in CGI.h) */
  55. void
  56. CustomCGIProcess ( CGIHdl theCGIHandle )
  57. {
  58.     char *    myResponseData;
  59.     
  60.     /* debugging check to ensure that theCGIHandle is not NULL */
  61.     my_assert ( theCGIHandle != NULL,
  62.         "\pCustomCGIProcess: theCGIHandle is NULL" );
  63.     
  64.     (*theCGIHandle)->responseSize = gHTTPHeaderOKSize
  65.         + 142 /* size of my constant string */ + 1 /* null terminator */;
  66.     
  67.     (*theCGIHandle)->responseData = MemoryNewHandle ( (*theCGIHandle)->responseSize, nil );
  68.     if ( (*theCGIHandle)->responseData != NULL )
  69.     {
  70.         /* get a local pointer to the responseData to make things a little easier */
  71.         HLock ( (*theCGIHandle)->responseData );
  72.         myResponseData = *((*theCGIHandle)->responseData);
  73.         
  74.         strcpy ( myResponseData, (char *)gHTTPHeaderOK );
  75.         strcpy ( &(myResponseData[gHTTPHeaderOKSize]),
  76.             "\r<TITLE>Grant's CGI Framework</TITLE>\r<BODY>This framework doesn't do anything. You must add your own code to generate useful results.</BODY>\r" );
  77.         
  78.         HUnlock ( (*theCGIHandle)->responseData );
  79.     }
  80.     else
  81.     {
  82.         (*theCGIHandle)->responseSize = nil;
  83.     }
  84.     
  85.     /* you should try to make use of giving time,
  86.         especially in for and while loops or where you have to wait for
  87.         some other process to return data or finish a task. */
  88.     ProcessGiveTime ( nil );
  89. } /* CustomCGIProcess */
  90.  
  91.  
  92. /* this function will be called after the cgi result has been returned.
  93.     It will contain the same CGIHdl that was used for the CustomCGIProcess.
  94.     This function's prototype is defined in "CGI.h" */
  95. void
  96. CustomCGIPostProcess ( CGIHdl theCGIHdl )
  97. {
  98.     /* as an example, I'll log some info here */
  99.     LogStringBreakP ( "\pHere is a post-process log entry:" );
  100.     
  101.     #if kCompileWithCGIfull_request
  102.     /* now I'll try to log the full_request, if it's present */
  103.     if ( (*theCGIHdl)->full_request != NULL && ((*theCGIHdl)->full_request)[nil] != nil )
  104.     {
  105.         LogStringP ( "\p\tfull_request: " );
  106.         LogStringBreak ( (*theCGIHdl)->full_request );
  107.     }
  108.     #endif
  109. } /* CustomCGIPostProcess */
  110.  
  111.  
  112. /***  CUSTOM CGI INITIALIZATION  ***/
  113. #pragma segment Startup
  114.  
  115. /* Put any of the initialization you need done, here.
  116.     This function will be called once: in-between the startup
  117.     sequence and the main event loop.
  118.     Return true if the initialization was successful, otherwise false.
  119.     An initialization failure will result in the application quitting. */
  120. Boolean
  121. CustomCGIStartup ( void )
  122. {
  123.     return true;
  124. } /* CustomCGIStartup */
  125.  
  126.  
  127. /***  CUSTOM CGI CLEAN UP  ***/
  128. #pragma segment Main
  129.  
  130. /* This function is called at quitting time. Put any cleanup you need to do in it.
  131.     Return true if you are succeful.
  132.     Return false if you are unable to quit for some reason (this generally only
  133.     applies when the user cancels)
  134.     allowUserInteract specifies whether you can make user interface calls.
  135.     However, CGIs generally should not rely on any user interface calls, so don't
  136.     depend on them. */
  137. Boolean
  138. CustomCGIQuit ( Boolean allowUserInteract )
  139. {
  140.     return true;
  141. }/* CustomCGIQuit */
  142.  
  143.  
  144. #endif    /* kCompileWithCGICode */
  145.  
  146. /*** EOF ***/
  147.